import router, { asyncRoutes, constantRoutes } from '@/router'
export default {
  namespaced: true,
  state: {
    routes: [],
    roles: {}
  },
  mutations: {
    setRoutes(state, payload) {
      state.routes = payload
    },
    setRoles(state, payload) {
      state.roles = payload
    }
  },
  actions: {
    filterRoutes(ctx, roles) {
      const routes = asyncRoutes.filter(item => {
        return roles.menus.includes(item.meta.id)
      })
      router.addRoutes([...routes,
        { path: '*', redirect: '/404', hidden: true }
      ])
      ctx.commit('setRoutes', [...constantRoutes, ...routes])
      ctx.commit('setRoles', roles)
    }
  }
}

